package com.lagou.server;

import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;

/**
 * 编程实现服务器的主功能
 */
public class ServerView {

    /**
     * 使用合成复用原则
     */
    private ServerInitClose sic;
    private ServerDao sd;

    /**
     * 通过构造方法实现成员变量的初始化
     *
     * @param sic
     */
    public ServerView(ServerInitClose sic, ServerDao sd) {
        this.sic = sic;
        this.sd = sd;
    }

    /**
     * 自定义成员方法实现客户端发来消息的接收并处理
     */
    public void serverReceive() throws IOException, ClassNotFoundException {
        while (true) {
            UserMessage tum = (UserMessage) sic.getOis().readObject();
            System.out.println("接收到的消息是：" + tum);
            // 调用方法实现管理员账号和密码信息的校验
            if ("managerCheck".equals(tum.getType())) {
                if (sd.serverManagerCheck(tum.getUser())) {
                    tum.setType("success");
                    sic.getOos().writeObject(tum);
                    sic.getOos().flush();
                    managerMainview();
                }
            } else if ("userCheck".equals(tum.getType())) {
                if (sd.serverUserCheck(tum.getUser())) {
                    tum.setType("success");
                    sic.getOos().writeObject(tum);
                    sic.getOos().flush();
                    userModuleview(tum.getUser());
                }
            }else {
                tum.setType("fail");
                sic.getOos().writeObject(tum);
            }
            // 将校验结果发送给客户端
            System.out.println("服务器发送校验结果成功！");
        }

    }

    /**
     * 自定义成员方法实现客户端发来消息的接收并选择相应的管理模块
     */

    public void managerMainview() throws IOException, ClassNotFoundException {
        while (true) {
            int choose = sic.getOis().read();
            switch (choose) {
                case 1:
                    userManagermentModuleView();
                    break;
                case 2:
                    examQuestionsManagermentModuleView();
                    break;
                case 0:
                    System.out.println("正在退出登陆...");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    public void userManagermentModuleView() throws IOException, ClassNotFoundException {
        sd.getUserInfo();
        while (true) {
            int choose = sic.getOis().read();
            switch (choose) {
                case 1:
                    sd.addUserInfo();
                    System.out.println("添加学员信息...");
                    break;
                case 2:
                    sd.deleteUserInfo();
                    System.out.println("删除学员信息...");
                    break;
                case 3:
                    sd.updateUserInfo();
                    System.out.println("修改学员信息...");
                    break;
                case 4:
                    sd.queryUserInfo();
                    System.out.println("查询学员信息...");
                    break;
                case 0:
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
            sd.saveUserInfo();
        }
    }

    public void examQuestionsManagermentModuleView() throws IOException, ClassNotFoundException {
        while (true) {
            sd.getQuestionsInfo();
            int choose = sic.getOis().read();
            switch (choose) {
                case 1:
                    sd.addExamQuestions();
                    System.out.println("增加考题...");
                    break;
                case 2:
                    sd.deleteExamQuestions();
                    System.out.println("删除考题...");
                    break;
                case 3:
                    sd.updateExamQuestions();
                    System.out.println("修改考题...");
                    break;
                case 4:
                    sd.queryExamQuestions();
                    System.out.println("查询考题...");
                    break;
                case 5:
                    sd.importExamQuestions();
                    System.out.println("导入考题...");
                    break;
                case 0:
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
            sd.saveQuestionsInfo();
        }
    }

    /**
     * 自定义成员方法实现客户端发来消息的接收并选择相应的管理模块
     */
    public void userModuleview(User user) throws IOException, ClassNotFoundException {
        while (true) {
            int choose = sic.getOis().read();
            switch (choose) {
                case 1:
                    examModuleview(user);
                    break;
                case 2:
                    sd.setPassword(user);
                    System.out.println("修改");
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    public void examModuleview(User user) throws IOException, ClassNotFoundException {
        sd.getQuestionsInfo();
        while (true) {
            int choose = sic.getOis().read();
            switch (choose) {
                case 1:
                    sd.startExam(user);
                    break;
                case 2:
                    sd.queryScoresInfo(user);
                    break;
                case 3:
                    sd.exportScoreInfo(user);
                    break;
                case 0:
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }


}

